package com.sjtu.bookread;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.sjtu.bookread.Fragment.BookShelfFragment;
import com.sjtu.bookread.Fragment.RankingFragment1;
import com.sjtu.bookread.Fragment.RecomFragment;
import com.sjtu.bookread.Fragment.TabFragmentStatePagerAdapter;
import com.sjtu.bookread.obj.BookObj1;

import java.util.ArrayList;
import java.util.List;

//import com.sjtu.bookread.Fragment.CategoryFragment;
//import com.sjtu.bookread.Fragment.RankingFragment;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private String user_name;

    private RadioGroup radioGroup;
    private TextView title;
    private ImageView search;
    private ImageView mine;

    private RadioGroup bottomRG;
    private RadioButton bookshelfRB;
    private RadioButton rankingRB;
    private RadioButton categoryRB;
    private RadioButton recomRB;

    private RadioGroup topRG;
    private RadioButton maleRB;
    private RadioButton femaleRB;
    private static Context context = null;

    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private TabFragmentStatePagerAdapter fragmentStatePagerAdapter;

    private SensorManager sensorManager;
    private Sensor sensor;
    public static float ax;
    public static float ay;
    public static float az;

    public static List<BookObj1> myBooks;

    public static Context getContext() {
        return context;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        changeStatusIconCollor(true);
        Window window =this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this,R.color.colorGreyTitle));

        setContentView(R.layout.activity_main);
        context = this.getBaseContext();

        //从传递的参数中获取章节相关信息
        Intent intent = this.getIntent();
        Bundle bundle1 = intent.getExtras();
        user_name = bundle1.getString("user_name");
//        user_name = "haha";

        //初始化加速度传感器
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (null == sensorManager) {
            Log.d("MainActivity", "deveice not support SensorManager");
        }
        sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMAL);

        //初始化控件
        title = findViewById(R.id.main_title);
        search = findViewById(R.id.main_search);
        bottomRG = findViewById(R.id.main_bottomRG);
        bookshelfRB = findViewById(R.id.main_bottom_bookshelf);
        rankingRB = findViewById(R.id.main_bottom_ranking);
        recomRB = findViewById(R.id.main_bottom_rec);
        mine = findViewById(R.id.main_mine);
        viewPager = findViewById(R.id.main_viewPager);

        //初始化Fragment
        BookShelfFragment bookShelfFragment = new BookShelfFragment();
        RankingFragment1 rankingFragment = new RankingFragment1();
        RecomFragment recomFragment = new RecomFragment();
        fragmentList.add(bookShelfFragment);
        fragmentList.add(rankingFragment);
        fragmentList.add(recomFragment);

        viewPager.setOnPageChangeListener(new MyPagerChangeListener());
        fragmentStatePagerAdapter = new TabFragmentStatePagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(fragmentStatePagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(2);

        //设置底部按钮图标大小
        Drawable drawable = getResources().getDrawable(R.mipmap.bookshelf_red);
        drawable.setBounds(0, 0, 70, 70);
        bookshelfRB.setCompoundDrawables(null, drawable , null,null);
        drawable = getResources().getDrawable(R.mipmap.ranking);
        drawable.setBounds(0, 0, 70, 70);
        rankingRB.setCompoundDrawables(null, drawable, null,null);
        drawable = getResources().getDrawable(R.mipmap.recom);
        drawable.setBounds(0, 0, 70, 70);
        recomRB.setCompoundDrawables(null, drawable,null, null);

        //处理我的事件
        mine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MineActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("user_name", user_name);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        //处理底部RG事件
        bottomRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index = 0;
                switch (checkedId) {
                    case R.id.main_bottom_bookshelf:
                        index = 0;
                        Drawable drawable = getResources().getDrawable(R.mipmap.bookshelf_red);
                        drawable.setBounds(0, 0, 70, 70);
                        bookshelfRB.setCompoundDrawables(null, drawable , null,null);
                        drawable = getResources().getDrawable(R.mipmap.ranking);
                        drawable.setBounds(0, 0, 70, 70);
                        rankingRB.setCompoundDrawables(null, drawable, null,null);
                        drawable = getResources().getDrawable(R.mipmap.recom);
                        drawable.setBounds(0, 0, 70, 70);
                        recomRB.setCompoundDrawables(null, drawable,null, null);
                        break;
                    case R.id.main_bottom_ranking:
                        index = 1;
                        drawable = getResources().getDrawable(R.mipmap.bookshelf);
                        drawable.setBounds(0, 0, 70, 70);
                        bookshelfRB.setCompoundDrawables(null, drawable , null,null);
                        drawable = getResources().getDrawable(R.mipmap.ranking_red);
                        drawable.setBounds(0, 0, 70, 70);
                        rankingRB.setCompoundDrawables(null, drawable, null,null);
                        drawable = getResources().getDrawable(R.mipmap.recom);
                        drawable.setBounds(0, 0, 70, 70);
                        recomRB.setCompoundDrawables(null, drawable,null, null);
                        break;
                    case R.id.main_bottom_rec:
                        index = 3;
                        drawable = getResources().getDrawable(R.mipmap.bookshelf);
                        drawable.setBounds(0, 0, 70, 70);
                        bookshelfRB.setCompoundDrawables(null, drawable , null,null);
                        drawable = getResources().getDrawable(R.mipmap.ranking);
                        drawable.setBounds(0, 0, 70, 70);
                        rankingRB.setCompoundDrawables(null, drawable, null,null);
                        drawable = getResources().getDrawable(R.mipmap.recom_red);
                        drawable.setBounds(0, 0, 70, 70);
                        recomRB.setCompoundDrawables(null, drawable,null, null);
                        break;
                }
                viewPager.setCurrentItem(index);
            }
        });

        //搜索按钮点击事件
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SearchActivity.class));
            }
        });
    }

    public void changeStatusIconCollor(boolean setDark) {
        View decorView = getWindow().getDecorView();
        if(decorView != null){
            int vis = decorView.getSystemUiVisibility();
            if(setDark){
                vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            } else{
                vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            decorView.setSystemUiVisibility(vis);
        }
    }

    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }
        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    title.setText("书架");
                    bookshelfRB.setChecked(true);
                    break;
                case 1:
                    title.setText("排行榜");
                    rankingRB.setChecked(true);
                    break;
                case 2:
                    title.setText("推荐");
                    recomRB.setChecked(true);
            }
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor == null)
            return ;
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            ax = event.values[0];
            ay = event.values[1];
            az = event.values[2];
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
